<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="90%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">

            <el-form-item label="显示/隐藏引导页" prop="status">
                <el-select v-model="ruleForm.status" class="w100p" clearable filterable placeholder="请选择">
                    <el-option label="显示" :value="true"></el-option>
                    <el-option label="隐藏" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="视频文件" prop="filesId">
                <UpLoadFiles v-if="dialogVisible" @getFileIdList="(e) => { getFileIdList(e, 'filesId') }"
                    :idList="videoFileIdList" :limit="1">
                </UpLoadFiles>
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="logo" prop="logo">
                        <UpLoadImage v-if="dialogVisible" @getFileIdList="(e) => { getFileIdList(e, 'logo') }"
                            :idList="logofileIdList" :limit="1">
                        </UpLoadImage>
                        <div>建议上传1920*1080px的图片</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="引导页背景图" prop="backgroundImage">
                        <UpLoadImage v-if="dialogVisible"
                            @getFileIdList="(e) => { getFileIdList(e, 'backgroundImage') }" :idList="fileIdList"
                            :limit="1">
                        </UpLoadImage>
                        <div>建议上传300*70px的图片</div>
                    </el-form-item></el-col>
            </el-row>
            <el-form-item label="引导页内容" prop="content">
                <!-- <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="ruleForm.content" class="w100p"
                    clearable placeholder="请输入"></el-input> -->
                <Ueditor v-if="resetUEditor" :content="ruleForm.content || ''" @text_content="update"></Ueditor>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import UpLoadFiles from "@/components/UpLoadFiles";
import Ueditor from '@/components/Ueditor';

import loadPage from "@/api/loadPage"
export default {
    components: { UpLoadImage, UpLoadFiles, Ueditor },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            resetUEditor: true,
            enumList: [],
            videoFileIdList: [],
            fileIdList: [],
            logofileIdList: [],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                content: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                displayLocation: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择', trigger: 'change' }
                ]
            }
        }
    },
    computed: {},
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.$set(this.ruleForm, 'status', true);
                this.addEditApi = loadPage.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.fileIdList = this.data.backgroundImage ? [this.data.backgroundImage] : [];
                this.videoFileIdList = this.data.filesId ? [this.data.filesId] : [];
                this.logofileIdList = this.data.logo ? [this.data.logo] : [];
                console.log(this.logofileIdList)
                this.addEditApi = loadPage.edit;
                this.addEditName = '编辑';
            }
            this.resetUEditor = false;
            this.$nextTick(() => {
                this.resetUEditor = true;
            });
        },
        //获取文章
        update(e) {
            this.$set(this.ruleForm, 'content', e)
        },
        getFileIdList(data, key) {
            let id = data.fileIdList && data.fileIdList.length > 0 ? data.fileIdList[0] : ''
            this.$set(this.ruleForm, key, id);
            console.log(this.ruleForm, data)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let success = res.code == 0;
                        let msg = success ? '操作成功' : '操作失败';
                        this.$message[success ? 'success' : 'warning'](res.msg || msg);
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err);
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileIdList = [];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>